<template>
	<el-card class="mod__menu" shadow="never">
		<el-form :inline="true">
			<el-form-item>
				<el-button type="primary" @click="handleMenu()">新增</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="danger" @click="handleToggle()">
					<el-icon style="width: 100%"><Sort />展开/折叠</el-icon>
				</el-button>
			</el-form-item>
		</el-form>
		<el-table
			v-if="refreshTable"
			v-loading="state.loading"
			:default-expand-all="isExpandAll"
			:data="state.dataList"
			row-key="id"
			border
			style="width: 100%">
			<el-table-column prop="name" label="名称" header-align="center" width="130"></el-table-column>
			<el-table-column prop="icon" label="图标" header-align="center" align="center" width="80">
				<template #default="scope">
					<svg-icon :icon="scope.row.icon"></svg-icon>
				</template>
			</el-table-column>
			<el-table-column prop="type" label="类型" header-align="center" align="center" width="100">
				<template #default="scope">
					<el-tag v-if="scope.row.type === 0" type="info">菜单</el-tag>
					<el-tag v-if="scope.row.type === 1" type="success">按钮</el-tag>
					<el-tag v-if="scope.row.type === 2" type="warning">接口</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="openStyle" label="打开方式" header-align="center" align="center" width="100">
				<template #default="scope">
					<span v-if="scope.row.type !== 0"></span>
					<el-tag v-else-if="scope.row.openStyle === 0">内部打开</el-tag>
					<el-tag v-else type="info">外部打开</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="sort" label="排序" header-align="center" align="center" width="70"></el-table-column>
			<el-table-column prop="url" label="路由" header-align="center" align="center" width="150" :show-overflow-tooltip="true"></el-table-column>
			<!-- <el-table-column prop="authority" label="授权标识" header-align="center" align="left" :show-overflow-tooltip="true"></el-table-column> -->
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-button type="primary" link @click="handleMenu(scope.row.id)">修改</el-button>
					<el-button type="primary" link @click="deleteMenu(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<MenuDlg ref="menuDlgRef" @refresh="getDataList" />
	</el-card>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { menuStore } from '@/store/menu'
import MenuDlg from './menu_dlg.vue'

const useMenuStore = menuStore()

const state = reactive({
	loading: false,
	dataList: [],
})

// 是否展开，默认全部折叠
const isExpandAll = ref(false)
// 是否重新渲染表格状态
const refreshTable = ref(true)

onMounted(() => {
	getDataList()
})

const getDataList = async () => {
	state.loading = true
	await useMenuStore.fetchMenuTree()
	state.dataList = useMenuStore.menuTree
	state.loading = false
	refreshTable.value = false
	setTimeout(() => {
		refreshTable.value = true
	}, 300)
}

const handleToggle = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

const menuDlgRef = ref(null) // 弹窗开关
const handleMenu = (id) => {
	// 新增
	if (!id) {
		menuDlgRef.value.init()
	} else {
		menuDlgRef.value.init(id)
	}
}

</script>

<style lang="scss" scoped>

</style>